package com.itheima.edu.info.manager.service;

import com.itheima.edu.info.manager.dao.TeacherDao;
import com.itheima.edu.info.manager.domain.Teacher;

public class TeacherService {
    private static TeacherDao teacherDao = new TeacherDao();

    //判断学号在库中存不存在
    public boolean isExists(String id) {
        Teacher[] teachersArray = teacherDao.findAllTeacher();
        //遍历并进行判断学号是否存在
        //假定不存在
        boolean flag = false;
        for (int i = 0; i < teachersArray.length; i++) {
            Teacher teacher = teachersArray[i];
            if (teacher != null && teacher.getId().equals(id)) {
                flag = true;
                break;
            }
        }
        return flag;
    }

    //调用Dao添加老师进库
    public boolean addTeacher(Teacher teacher) {
        return teacherDao.addTeacher(teacher);
    }

    //查询老师,判断库中是否有元素
    public Teacher[] findAllTeacher() {
        //1.调用Dao的findAllTeacher方法获取库中数组
        Teacher[] teacherArray = teacherDao.findAllTeacher();
        //2.判断teacherArray中是否含有元素
        boolean flag = false;
        for (int i = 0; i < teacherArray.length; i++) {
            Teacher teacher = teacherArray[i];
            if (teacher != null) {
                flag = true;
                break;
            }
        }
        if (flag) {
            //2.1有元素即返回teacherArray
            return teacherArray;
        } else {
            //2.2无元素,返回null
            return null;
        }
    }

    //删除老师
    public void deleteTeacher(String id) {
        //拿到数组
        Teacher[] allTeacher = teacherDao.findAllTeacher();
        //调用Dao的delete方法
        teacherDao.deleteTeacher(id);
    }

    //修改信息
    public void updateTeacherId(Teacher teacher, String id) {
        teacherDao.updateTeacherId(teacher, id);
    }
}
